package com.gdth.bank.presenter;

import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import com.gdth.bank.model.ServerModel;
import com.gdth.bank.utils.ServerUtil;
import com.gdth.bank.view.ServerView;

/** 
 * @author  Arvin: 
 * @version 2016年5月28日 上午10:17:47 
 * 
 */
public class ServerPresener<T> {
	private ServerView mServerView;
	private ServerModel<T> mServerModel;
	
	public ServerPresener(ServerView mServerView){
		this.mServerView = mServerView;
		this.mServerModel = new ServerModel<T>();
	}
	
	public void save(T t){
		mServerView.showProgressDialog("正在保存");
		mServerModel.save(t)
			.subscribeOn(Schedulers.io())
			.unsubscribeOn(Schedulers.io())
			.observeOn(AndroidSchedulers.mainThread())
			.subscribe(new ServersSubscriber());
	}
	
	class ServersSubscriber extends Subscriber<Long>{

		@Override
		public void onCompleted() {
			// TODO Auto-generated method stub
			mServerView.hideProgressDialog();
			this.unsubscribe();
		}

		@Override
		public void onError(Throwable paramThrowable) {
			// TODO Auto-generated method stub
			mServerView.hideProgressDialog();
			this.unsubscribe();
			mServerView.showMessage("保存失败");
		}

		@Override
		public void onNext(Long paramT) {
			// TODO Auto-generated method stub
			mServerView.updateView(ServerUtil.server_save, paramT);
		}

	}

	public void loadAll(T t) {
		mServerView.showProgressDialog("正在加载");
		mServerModel.loadAll(t)
			.subscribeOn(Schedulers.io())
			.observeOn(AndroidSchedulers.mainThread())
			.subscribe(new ServerloadAllSubscriber());
	}
	
	class ServerloadAllSubscriber extends Subscriber<List<T>>{

		@Override
		public void onCompleted() {
			// TODO Auto-generated method stub
			mServerView.hideProgressDialog();
			this.unsubscribe();
		}

		@Override
		public void onError(Throwable paramThrowable) {
			// TODO Auto-generated method stub
			mServerView.hideProgressDialog();
			this.unsubscribe();
			mServerView.showMessage("加载数据失败");
		}

		@Override
		public void onNext(List<T> paramT) {
			// TODO Auto-generated method stub
			mServerView.updateView(ServerUtil.server_loadAll, paramT);
		}
		
	}

	public void delete(T t){
		mServerView.showProgressDialog("删除中...");
		mServerModel.delete(t)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread())
		.subscribe(new ServerDeleteSubscriber());
	}
	
	class ServerDeleteSubscriber extends Subscriber<T>{

		@Override
		public void onCompleted() {
			// TODO Auto-generated method stub
			mServerView.hideProgressDialog();
			this.unsubscribe();
		}

		@Override
		public void onError(Throwable paramThrowable) {
			// TODO Auto-generated method stub
			mServerView.hideProgressDialog();
			this.unsubscribe();
		}

		@Override
		public void onNext(T paramT) {
			// TODO Auto-generated method stub
			mServerView.updateView(ServerUtil.server_delete, paramT);
		}
		
		
	}

	public void findById(Long id, T t) {
		mServerView.showProgressDialog("查找中...");
		mServerModel.findById(id, t)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread())
		.subscribe(new ServerFindByIdSubscriber());
		
	}
	
	class ServerFindByIdSubscriber extends Subscriber<T>{

		@Override
		public void onCompleted() {
			// TODO Auto-generated method stub
			mServerView.hideProgressDialog();
			this.unsubscribe();
		}

		@Override
		public void onError(Throwable paramThrowable) {
			// TODO Auto-generated method stub
			mServerView.hideProgressDialog();
			this.unsubscribe();
		}

		@Override
		public void onNext(T paramT) {
			// TODO Auto-generated method stub
			mServerView.updateView(ServerUtil.server_find, paramT);
		}
		
		
	}
}
